import { createRouter, createWebHashHistory } from "vue-router";
import type { RouteRecordRaw } from "vue-router";
import { useUserStore } from "@/store";

const routes: RouteRecordRaw[] = [
  {
    path: "/",
    redirect: "/login",
  },
  {
    path: "/login",
    component: () => import("../pages/LoginPage.vue"),
  },
  {
    path: "/register",
    component: () => import("../pages/RegisterPage.vue"),
  },
  {
    path: "/registerSuccess",
    component: () => import("../pages/RegisterSuccessPage.vue"),
  },
  {
    path: "/home",
    component: () => import("../pages/HomePage.vue"),
  },
  {
    path: "/evaluation",
    component: () => import("../pages/EvaluationDataPage.vue"),
  },
  {
    path: "/evaluationResult/:id?",
    component: () => import("../pages/EvaluationResult.vue"),
  },
  /**
   * 这里表示只有isExpert才能进入这个页面
   */
  {
    path: "/expertEvaluationResult",
    component: () => import("../pages/DiscussionPage.vue"),
    // 专家测评也改成讨论页面
    beforeEnter: (to, from, next) => {
      const userStore = useUserStore();
      if (userStore.isExpert) {
        next(); // 允许访问
      } else {
        alert("您没有权限访问该页面，请联系系统管理员！");
        next("/home"); // 重定向到/home
      }
    },
  },
  {
    path: "/mine",
    component: () => import("../pages/MinePage.vue"),
  },
  {
    path: "/discussion",
    component: () => import("../pages/DiscussionPage.vue"),
  },
  {
    path: "/suggest",
    component: () => import("../pages/SuggestPage.vue"),
  },
  {
    path: "/manageUsers",
    component: () => import("../pages/admin/ManageUser.vue"),
  },
  {
    path: "/suggestList",
    component: () => import("../pages/admin/ShowSuggestions.vue"),
  },
  // {
  //   path: "/precinctRecords",
  //   component: () => import("../pages/PrecinctRecords.vue"),
  // },
];

const router = createRouter({
  routes,
  history: createWebHashHistory(),
});

export default router;
